package com.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import com.model.Student;

public class StudentDao {
	
	public List<Student> getAllStudent()
	{
		List<Student> list =new ArrayList<Student>();
		Connection conn = DataBase.getConnection();
		try {
			
			Statement smt = conn.createStatement();
			String sql="select * from StudentInfo";
			ResultSet rs = smt.executeQuery(sql);
			while(rs.next())
			{
				Student stu = new Student();
				stu.setStu_xh(rs.getString("stu_xh"));
				stu.setStu_name(rs.getString("stu_name"));
				stu.setStu_sex(rs.getString("stu_sex"));
				stu.setStu_age(rs.getInt("stu_age"));
				stu.setStu_mobile(rs.getString("stu_mobile"));
				stu.setStu_address(rs.getString("stu_address"));
				stu.setClass_id(rs.getString("class_id"));
				list.add(stu);
			}
			rs.close();
			smt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;
	}
	
	public Student getOneStudentByXh(String xh)
	{
		Student stu=null;
		Connection conn = DataBase.getConnection();
		try {
			
			Statement smt = conn.createStatement();
			String sql="select * from StudentInfo where stu_xh='"+xh+"'";
			ResultSet rs = smt.executeQuery(sql);
			if(rs.next())
			{
				stu = new Student();
				stu.setStu_xh(rs.getString("stu_xh"));
				stu.setStu_name(rs.getString("stu_name"));
				stu.setStu_sex(rs.getString("stu_sex"));
				stu.setStu_age(rs.getInt("stu_age"));
				stu.setStu_mobile(rs.getString("stu_mobile"));
				stu.setStu_address(rs.getString("stu_address"));
				stu.setClass_id(rs.getString("class_id"));
			
			}
			rs.close();
			smt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return stu;
	}
	
	
	public boolean addStudent(Student stu)
	{
		boolean flag = false;
		Connection conn = DataBase.getConnection();
		try {
			
			Statement smt = conn.createStatement();
			String sql="insert into StudentInfo(stu_xh,stu_name,stu_sex,stu_age,stu_mobile,stu_address,class_id) values (";
			sql+="'"+stu.getStu_xh()+"',";
			sql+="'"+stu.getStu_name()+"',";
			sql+="'"+stu.getStu_sex()+"',";
			sql+="'"+stu.getStu_age()+"',";
			sql+="'"+stu.getStu_mobile()+"',";
			sql+="'"+stu.getStu_address()+"',";
			sql+="'"+stu.getClass_id()+"'";
			sql+=")";
			System.out.println(sql);
			int num = smt.executeUpdate(sql);
			if(num>0)
			{
				flag = true;
			}
			smt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}
	
	public boolean deteteStudentByXh(String xh)
	{
		boolean flag = false;
		Connection conn = DataBase.getConnection();
		try {
			
			Statement smt = conn.createStatement();
			String sql="delete from StudentInfo where stu_xh='"+xh+"'";
			
			int num = smt.executeUpdate(sql);
			if(num>0)
			{
				flag = true;
			}
			smt.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return flag;
	}
}
